@file:Suppress("DEPRECATION")

package com.starsea.book.ui.book.toc

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.activityViewModels
import androidx.viewpager.widget.ViewPager
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentDrawerChapterListBinding
import com.starsea.book.ui.book.toc.rule.TxtTocRuleDialog
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * 目录
 */
class TocFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_drawer_chapter_list),
    TxtTocRuleDialog.CallBack {

    private val binding by viewBinding(FragmentDrawerChapterListBinding::bind)
    override val viewModel by activityViewModels<TocViewModel>()
    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
        binding.viewPager.adapter = TabFragmentPageAdapter()

        binding.rtvToc.setOnClickListener {
            binding.viewPager.currentItem = 0
            binding.rtvToc.isSelected = true
            binding.rtvBookMark.isSelected = false
        }
        binding.rtvBookMark.setOnClickListener {
            binding.viewPager.currentItem = 1

            binding.rtvToc.isSelected = false
            binding.rtvBookMark.isSelected = true
        }

        binding.viewPager.currentItem = 0
        binding.rtvToc.isSelected = true
        binding.rtvBookMark.isSelected = false

        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                binding.rtvToc.isSelected = position == 0
                binding.rtvBookMark.isSelected = position == 1
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })


        viewModel.bookData.value?.let { book ->
            binding.ivCover.loadNormalImage(requireContext(), book.coverUrl ?: "")
            binding.tvBookTitle.text = book.name
        }

    }

    companion object {
        fun newInstance(bookUrl: String): TocFragment {
            val args = Bundle()
            val fragment = TocFragment()
            args.putString("bookUrl", bookUrl)
            fragment.arguments = args
            return fragment
        }
    }


    override fun onTocRegexDialogResult(tocRegex: String) {
//        viewModel.bookData.value?.let { book ->
//            book.tocUrl = tocRegex
//            upBookAndToc(book)
//        }
    }


    @Suppress("DEPRECATION")
    private inner class TabFragmentPageAdapter :
        FragmentPagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

        override fun getItem(position: Int): Fragment {
            return when (position) {
                1 -> BookmarkFragment()
                else -> ChapterListFragment()
            }
        }

        override fun getCount(): Int {
            return 2
        }

        override fun getPageTitle(position: Int): CharSequence {
            return when (position) {
                1 -> getString(R.string.bookmark)
                else -> getString(R.string.chapter_list)
            }
        }

    }

}